var UM_OB = new function () {
	hst		= 'http://www.uread.me';
	doc		= null;
	xmlhttp 	= new XMLHttpRequest();
	hs		= '';
	
	this.opn = function () {
		doc = window.getBrowser().selectedBrowser.contentDocument;
		if (doc.URL.search(/[http:|https:]/i) == 0) {
			hs 	= get_selection_HTML();
			if (hs) {
				get_is_login('init');
			} else {
				if (doc.readyState == 'complete') {
					hs = get_all_HTML();
					get_is_login('init');
				} else {
					create_error('当前网页尚未加载完毕，正在等待加载完成...', '或者您可以选中部分内容进行保存。');
					return false;
				}
			}
		} else {
			window.getBrowser().selectedBrowser.loadURI('http://uread.me');
		}
	}
	get_is_login = function (flag) {
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				if (xmlhttp.status==200) {
					if (flag == 'init') {		// 初始状态下，显示save或者login页面
						if (xmlhttp.responseText == 'true') {
							get_json_save();		
						} else {
							create_login('check');
						}
					} else {			// 其他状态下，仅仅是check，成功后显示save，否则没有操作
						if (xmlhttp.responseText == 'true') {
							get_json_save();
						} else {
							// 没有结果的时候不用操作，继续等待下一次检查
						}
					}
				} else {
					create_error('未能连接到服务器！', '');
				}
			}
		}
		xmlhttp.open("GET", hst+"/is_login.php?br=cr&flag="+flag+"&time="+(+new Date), true);
		xmlhttp.send();
	}
	get_json_save = function () {
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				if (xmlhttp.status==200) {
					try {
						var json = JSON.parse(xmlhttp.responseText);
					} catch (e) {
						json = JSON.parse( '{"error":"can not parse json"}' );
					}
					if (json != null && json.nick) {
						create_tpl(json);
					} else {
						create_error('未能获得服务器数据！', '');
					}
				} else {
					create_error('未能连接到服务器！', '');
				}
			}
		}
		xmlhttp.open("GET", hst+"/save.php?br=cr&time"+(+new Date), true);
		xmlhttp.send();
	}
	get_logout = function () {
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				if (xmlhttp.status==200) {
					if (xmlhttp.responseText == 'true') {
						create_login();
					}
				} else {
					create_error('未能连接到服务器！', '');
				}
			}
		}
		xmlhttp.open("GET", hst+"/logout.php?br=cr&time="+(+new Date), true);
		xmlhttp.send();
	}
	create_login = function (dinto) {
		var c = create_bgfg();
		var ifr = doc.createElement('iframe');
		ifr.setAttribute('style',	style_maker('display:block; background-color:#FFF; width:100%; height:100%;'));
		ifr.setAttribute('src', 	hst+'/login.php?ureadmeiframe=1&dinto='+dinto+'&time='+(+new Date));
		c.appendChild(ifr);
		ifr.onload = function() {
			get_is_login(dinto);
		}
	}
	create_tpl = function (json) {
		var content = create_bgfg();
		
		// 登录用户的昵称
		var a = doc.getElementById('uReadMe_nick');
		if (a) {
			a.appendChild(doc.createTextNode(json.nick + ' ')); 	// 讨厌的IE在处理中英文字符时候总是差一个像素，加个空格暂时解决一下
		}
		// 退出的链接
		a = doc.getElementById('uReadMe_exit');
		if (a) {
			a.appendChild(doc.createTextNode('退出 ')); 	// 讨厌的IE在处理中英文字符时候总是差一个像素，加个空格暂时解决一下
		}
		// 已经登录的账号icon
		a = doc.getElementById('uReadMe_is_login');
		if (a && json.is_login) {
			for (i=0; i<json.is_login.length; i++) {
				b = doc.createElement('img');
				b.setAttribute('style',	style_maker('display:inline-block; margin:0 4px 0 0;'));
				b.setAttribute('title',	json.is_login[i].name + '已经登录成功');
				b.setAttribute('src',	json.is_login[i].img);
				a.appendChild(b);
			}
		}
		// 未登录的账号icon和登陆提示和登陆链接
		a = doc.getElementById('uReadMe_not_login');
		if (a && json.not_login && json.not_login.length>0) {
			for (i=0; i<json.not_login.length; i++) {
				b = doc.createElement('img');
				b.setAttribute('style',	style_maker('display:inline-block; margin:0 4px 0 0;'));
				b.setAttribute('title',	'授权过期，请重新登陆');
				b.setAttribute('src',	json.not_login[i].img);
				a.appendChild(b);
			}
//			<div style="position:absolute; text-align: left; top:22px;">
//				<img src="/images/arrow_tips_up.png" style="margin:0 0 -1px 5px; width:13px; height:7px;">
//				<div style="border:1px solid #CCA; background-color: #FFC; padding:10px 20px 20px 20px; white-space: nowrap;">
//					绑定账号的登录授权已过期
//					<{section name=i loop=$not_login}>
//						<div style="margin-top:10px;"><{$not_login[i].link}></div>
//					<{/section}>
//				</div>
//			</div>

			b = doc.createElement('div');
			b.setAttribute('id',	'uReadMe_not_login_tips');
			b.setAttribute('style',	style_maker('position:absolute; text-align: left; top:22px; left:' + (a.offsetLeft-3) + 'px;'));
			c = doc.createElement('img');
			c.setAttribute('style',	style_maker('margin:0 0 -1px 5px; width:13px; height:7px;'));
			c.setAttribute('src',	'http://uread.me/images/arrow_tips_up.png');
			b.appendChild(c);
			
			c = doc.createElement('div');
			c.setAttribute('style',	style_maker('display:block; border:1px solid #CCA; background-color: #FFC; padding:10px 20px; color:#ACF;'));
			c.appendChild(doc.createTextNode('以下账号的登录授权已过期')); 
			for (i=0; i<json.not_login.length; i++) {
				d = doc.createElement('img');
				d.setAttribute('style',	style_maker('border:none; cursor:pointer; display:block; margin:10px 0 0 0;'));
				d.setAttribute('src',	json.not_login[i].login_img);
				d.setAttribute('id',	json.not_login[i].flag);
				d.onclick = function () {
					create_login(this.id);
				}
				c.appendChild(d);
			}
			d = doc.createElement('div');
			d.setAttribute('style',	style_maker('display:block; margin:15px 0 0 0; text-align:right;'));
			e = doc.createElement('a');
			e.setAttribute('style',	style_maker('color:#ACF; text-decoration:underline; cursor:pointer;'));
			e.appendChild(doc.createTextNode('关闭')); 
			e.onclick = function () {
				doc.getElementById('uReadMe_not_login_tips').style.display = 'none';
			}
			d.appendChild(e);
			c.appendChild(d);
			b.appendChild(c);
			a.appendChild(b);
		}
		
		// TITLE LINE ------------------------------------------------------------------------------------------------------------------------------------
		a = doc.createElement('div');
		a.setAttribute('style',	style_maker('display:block; margin:20px 20px 0 20px; padding:0 20px 5px 20px; border-bottom:1px dotted #CCC;'));
		content.appendChild(a);
			// title
			var b = doc.createElement('div');
			b.setAttribute('style',	style_maker('display:block; float:left; width:60%; font-size:18px; font-family:微软雅黑; padding:20px 0 0 0;'));
			b.appendChild(doc.createTextNode(doc.title)); 
			a.appendChild(b);
			// title right
			var c = doc.createElement('div');
			c.setAttribute('style',	style_maker('display:block; float:right; width:39%;'));
			a.appendChild(c);
				// save button
				d = doc.createElement('button');
				d.setAttribute('id',	'uReadMe_save_button');
				d.setAttribute('type',	'button');
				d.appendChild(doc.createTextNode('完成')); 
				d.setAttribute('style',	style_maker('display:inline-block; margin:0 0 0 10px; padding:5px 20px 4px 20px; font-size:18px; font-family:微软雅黑; color:#FFF; background-color:#69F; cursor:pointer; border-top:1px solid #CCC; border-left:1px solid #CCC; border-right:1px solid #333; border-bottom:1px solid #333;'));
				d.onclick = function () {
					save();
				}
				c.appendChild(d);
				// 设定 right部分的margin top以适应标题的底部对齐
				c.setAttribute('style',	style_maker('display:block; float:right; width:39%; margin-top:'+(b.clientHeight-c.clientHeight)+'px;'));
		// BODY LINE ------------------------------------------------------------------------------------------------------------------------------------
		var h = content.clientHeight - a.clientHeight - 50;
		a = doc.createElement('div');				// body
		a.setAttribute('id',	'uReadMe_body');
		a.setAttribute('style',	style_maker('display:block; padding:5px 20px; height:'+h+'px '));
		content.appendChild(a);
			// 圈子行 ------------------------------------------------------------------------------------------------------------------------------------
			var b = doc.createElement('div');
			b.setAttribute('style',	style_maker('display:block; margin:0 20px; padding:20px 0px 5px 0px; border-bottom:1px dotted #CCC;'));
			a.appendChild(b);
				// circle caption ------------------------------------------------------------------------------------------------------------------------------------
				c = doc.createElement('div');
				c.setAttribute('style',	style_maker('color:#777777; padding:10px 0 0 0; float:left; text-align:right; width:100px;'));
				c.appendChild(doc.createTextNode('保存到阅读圈：')); 
				b.appendChild(c);
				// circle list ------------------------------------------------------------------------------------------------------------------------------------
				c = doc.createElement('div');
				c.setAttribute('style',	style_maker('display:block; margin:0 0 0 110px; min-height:40px;'));
				b.appendChild(c);
					// circle name ------------------------------------------------------------------------------------------------------------------------------------
					if (typeof(json.circles) == 'object' && json.circles.length>0) {
						for (i=0; i<json.circles.length; i++) {
							b = doc.createElement('a');
							b.setAttribute('name',	'uReadMe_circles');
							b.setAttribute('uReadMeId',	json.circles[i].id);
							b.setAttribute('style',	style_maker('display:inline-block; font-size:14px; background-color:transparent; margin:5px 10px 0 0; padding:5px; text-decoration:underline; cursor:pointer;'));
							if (json.circles[i].privacy == 'private') {
								b.style.color = '#851';
								b.setAttribute('uReadMeColor',	'#851');
								b.setAttribute('uReadMeSBG',	'#D82');
							} else {
								b.style.color = '#069';
								b.setAttribute('uReadMeColor',	'#069');
								b.setAttribute('uReadMeSBG',	'#69F');
							}
							b.appendChild(doc.createTextNode(json.circles[i].name)); 
							b.onclick = function () {
								if (color2hex(this.style.backgroundColor) == 'transparent') {
									this.style.backgroundColor = '#69F';
									this.style.color = '#FFF';
								} else {
									this.style.backgroundColor = 'transparent';
									this.style.color = '#069';
								}
							}
							c.appendChild(b);
						}
						// 新建圈子 ------------------------------------------------------------------------------------------------------------------------------------
						b = doc.createElement('a');
						b.setAttribute('id',	'uReadMe_new_circle_a');
						b.setAttribute('style',	style_maker('display:inline-block; font-size:12px; color:#060; margin:5px 10px 1px 0; padding:5px; text-decoration:none; cursor:pointer;'));
						b.appendChild(doc.createTextNode('+新建阅读圈')); 
						b.onclick = function () {
							var tmp = doc.getElementById('uReadMe_new_circle_div');
							tmp.style.display = 'block';
							tmp.style.top = (this.offsetTop - 7) + 'px';
							if ( (this.offsetLeft + tmp.clientWidth) > content.clientWidth ) {
								tmp.style.left = (content.clientWidth - tmp.clientWidth - 30) + 'px';
							} else {
								tmp.style.left = this.offsetLeft + 'px';
							}
						}
						c.appendChild(b);
						var nc = doc.createElement('div');
						nc.setAttribute('id',	'uReadMe_new_circle_div');
						nc.setAttribute('style',	style_maker('display:none; position:absolute; font-size:12px; color:#060; padding:5px; border:1px solid #EEE; background-color:#FAFAFA;'));
						c.appendChild(nc);
						b = doc.createElement('input');				// input circle name
						b.setAttribute('id',	'uReadMe_new_circle_name');
						b.setAttribute('maxLength',	20);
						b.setAttribute('type',	'text');
						b.setAttribute('style',	style_maker('width:110px; height:17px; font-size:14px; border:1px solid #AAA; padding:2px; margin:0 10px;'));
						nc.appendChild(b);
						b = doc.createElement('input');
						b.setAttribute('id',	'uReadMe_new_circle_is_private');
						b.setAttribute('type',	'checkbox');
						b.setAttribute('style',	style_maker('display:inline-block; margin:0 0 1px 5px;'));
						nc.appendChild(b);
						b = doc.createElement('span');
						b.setAttribute('style',	style_maker('display:inline-block; font-size:12px; color:#060; padding:0 0 0 2px; cursor:pointer;'));
						b.appendChild(doc.createTextNode('私有')); 
						b.onclick = function () {
							var chk = doc.getElementById('uReadMe_new_circle_is_private');
							if (chk.checked) {
								chk.checked = false;
							} else {
								chk.checked = true;
							}
						}
						nc.appendChild(b); 
						b = doc.createElement('button');				// 
						b.setAttribute('style',	style_maker('padding:2px 10px; border:2px outset #060; background-color:#060; color:#FFF; margin:0 0 0 20px; cursor:pointer;'));
						b.appendChild(doc.createTextNode('确定')); 
						b.onclick = function () {
							var nm = doc.getElementById('uReadMe_new_circle_name').value;
							if (nm) {
								var na = doc.getElementById('uReadMe_new_circle_a');
								var p = na.parentNode;
								na.style.display = 'none';
								doc.getElementById('uReadMe_new_circle_div').style.display = 'none';

								var tmp = doc.createElement('a');
								tmp.setAttribute('style',	style_maker('display:inline-block; font-size:14px; color:#FFF; background-color:'+((doc.getElementById('uReadMe_new_circle_is_private').checked) ? '#D82' : '#69F')+'; margin:5px 10px 0 0; padding:5px; text-decoration:underline; cursor:pointer;'));
								tmp.appendChild(doc.createTextNode(nm)); 
								tmp.onclick = function () {
									doc.getElementById('uReadMe_new_circle_name').value = '';
									doc.getElementById('uReadMe_new_circle_is_private').checked = false;
									doc.getElementById('uReadMe_new_circle_a').style.display = 'inline-block';
									this.parentNode.removeChild(this);
								}
								p.appendChild(tmp);
							} else {
								alert('请填写圈子的名称！');
							}
						}
						nc.appendChild(b);
						b = doc.createElement('a');				// 
						b.setAttribute('style',	style_maker('padding:2px 10px; color:#060; margin:0 0 2px 20px; text-decoration:underline; cursor:pointer;'));
						b.appendChild(doc.createTextNode('取消')); 
						b.onclick = function () {
							doc.getElementById('uReadMe_new_circle_name').value = '';
							doc.getElementById('uReadMe_new_circle_is_private').checked = false;
							doc.getElementById('uReadMe_new_circle_div').style.display = 'none';
						}
						nc.appendChild(b);
					} else {
						var nc = doc.createElement('div');
						nc.setAttribute('id',	'uReadMe_new_circle_div');
						nc.setAttribute('style',	style_maker('display:inline-block; font-size:12px; color:#060; padding:5px 15px; border:1px solid #EEE; background-color:#FAFAFA;'));
						c.appendChild(nc);
						b = doc.createElement('input');				// input circle name
						b.setAttribute('id',	'uReadMe_new_circle_name');
						b.setAttribute('type',	'text');
						b.setAttribute('maxLength',	20);
						b.setAttribute('style',	style_maker('width:110px; height:17px; font-size:14px; border:1px solid #AAA; padding:2px;'));
						nc.appendChild(b);
						
						b = doc.createElement('input');				// input checkbox is private
						b.setAttribute('id',	'uReadMe_new_circle_is_private');
						b.setAttribute('type',	'checkbox');
						b.setAttribute('value',	'1');
						b.setAttribute('style',	style_maker('margin:0 2px 1px 5px;'));
						nc.appendChild(b);
						b = doc.createElement('span');
						b.setAttribute('style',	style_maker('display:inline-block; font-size:12px; color:#060; padding:0 0 0 2px; cursor:pointer;'));
						b.appendChild(doc.createTextNode('私有')); 
						b.onclick = function () {
							var chk = doc.getElementById('uReadMe_new_circle_is_private');
							if (chk.checked) {
								chk.checked = false;
							} else {
								chk.checked = true;
							}
						}
						nc.appendChild(b); 
					}
			// 好友行 ------------------------------------------------------------------------------------------------------------------------------------
			var b = doc.createElement('div');				// friend
			b.setAttribute('style',	style_maker('display:block; margin:0 20px; padding:10px 0px 5px 0px; border-bottom:1px dotted #CCC;'));
			a.appendChild(b);
				// friend caption ------------------------------------------------------------------------------------------------------------------------------------
				c = doc.createElement('div');
				c.setAttribute('style',	style_maker('color:#777777; padding:10px 0 0 0; float:left; text-align:right; width:100px;'));
				c.appendChild(doc.createTextNode('推荐给好友：')); 
				b.appendChild(c);
				// friend list ------------------------------------------------------------------------------------------------------------------------------------
				c = doc.createElement('div');				
                                c.setAttribute('id', 'uReadMe_friends_div');
				c.setAttribute('style',	style_maker('display:block; margin:0 0 0 110px; min-height:40px;'));
				b.appendChild(c);
					// friend name ------------------------------------------------------------------------------------------------------------------------------------
					if (typeof(json.friends) == 'object' && json.friends.length>0) {
						for (i=0; i<json.friends.length; i++) {
							b = doc.createElement('a');			// title
							b.setAttribute('style',	style_maker('display:inline-block; font-size:14px; color:#069; margin:5px 10px 0 0; padding:5px; text-decoration:underline; cursor:pointer;'));
							b.setAttribute('name',	'uReadMe_friends');
							b.setAttribute('uReadMeId',	json.friends[i].id);
                                                        b.setAttribute('uReadMeSocials',	json.friends[i].socials);
							b.appendChild(doc.createTextNode(json.friends[i].name)); 
							b.onclick = function () {
								if (color2hex(this.style.backgroundColor) == 'transparent') {
									this.style.backgroundColor = '#69F';
									this.style.color = '#FFF';
								} else {
									this.style.backgroundColor = 'transparent';
									this.style.color = '#069';
								}
							}
							b.onmouseover = function () {
								var a = this;
								uReadMe_TimeOut = setTimeout(  function () {float_social('show', a)}  , 300);
							}
							b.onmouseout = function () {
								clearTimeout(uReadMe_TimeOut);
								float_social('hide', this);
							}
							c.appendChild(b);
						}
					}
	}
	create_error = function (msg_red, msg_blue) {
		var c = create_bgfg();
		
		var d = doc.createElement('div');
		d.setAttribute('style',	style_maker('display:block; padding:100px 0 0 0; '));
		c.appendChild(d);
		if (msg_red) {
			var e = doc.createElement('div');
			e.setAttribute('style',	style_maker('display:block; font-size:32px; color:#FA6; font-family:微软雅黑,黑体; text-align:center; padding:0 0 10px 0;'));
			e.appendChild(doc.createTextNode(msg_red)); 
			d.appendChild(e);
		}
		if (msg_blue) {
			var f = doc.createElement('div');
			f.setAttribute('style',	style_maker('display:block; font-size:28px; color:#ACF; font-family:微软雅黑,黑体; text-align:center; padding:0 0 10px 0;'));
			f.appendChild(doc.createTextNode(msg_blue)); 
			d.appendChild(f);
		}
	}
	create_bgfg = function () {
		close_layer();
		var a = doc.createElement('div');
		a.setAttribute('id', 		'uReadMe_bg');
		a.setAttribute('style',	style_maker('display:block; background-color:#20292F; top:0; left:0; right:0; bottom:0; position:fixed; z-index:2147483647; opacity:0.7;'));
		a.onclick 		= function () { close_layer(); }
		doc.body.appendChild(a);
		a = doc.createElement('div');
		a.setAttribute('id', 		'uReadMe_fg');
		a.setAttribute('style',	style_maker('display:block; background-color:#F9F9F9; border-top:1px solid #FFF; border-right:1px solid #888; border-bottom:1px solid #666; border-left:1px solid #FFF; top:50px; left:10%; right:10%; bottom:50px; position:fixed; z-index:2147483647;'));
		doc.body.appendChild(a);
		
		var b = doc.createElement('div');		// header
		b.setAttribute('style',	style_maker('display:block; height:28px; background-color:#369; border-bottom:1px solid #CCC;'));
		a.appendChild(b);
		var c = doc.createElement('a');		// logo
		c.setAttribute('style',	style_maker('margin:8px 0 0 0; padding:0 20px; float:left; cursor:pointer;'));
		c.setAttribute('title', 'uRead.Me');
		c.setAttribute('href', 'http://uread.me');
		c.setAttribute('target', '_blank');
		var d = doc.createElement('img');		// logo image
		d.setAttribute('style',	style_maker('cursor:pointer; width:96px; height:12px;'));
		d.setAttribute('src', 'http://uread.me/images/logo_small.png');
		c.appendChild(d);
		b.appendChild(c);
		c = doc.createElement('a');		// close
		c.setAttribute('style',	style_maker('font-weight:bold; font-size:14px; color:#ACF; margin:8px 0 0 30px; padding:0 15px; text-decoration:none; float:right; cursor:pointer;'));
		c.appendChild(doc.createTextNode("X")); 
		c.onclick		= function () { close_layer(); }
		b.appendChild(c);
		c = doc.createElement('a');		// exit
		c.setAttribute('style',	style_maker('color:#ACF; margin:8px 0 0 30px; padding:0 15px; text-decoration:none; float:right; cursor:pointer; overflow:hidden;'));
		c.setAttribute('id', 		'uReadMe_exit');
		c.onclick		= function () { get_logout(); }
		b.appendChild(c);
		c = doc.createElement('a');		// nick
		c.setAttribute('style',	style_maker('color:#ACF; margin:8px 0 0 0; padding:0 15px; text-decoration:none; float:right; cursor:pointer; overflow:hidden;'));
		c.setAttribute('id', 		'uReadMe_nick');
		c.setAttribute('href', 		'http://uread.me');
		c.setAttribute('target', 	'_blank');
		b.appendChild(c);
		c = doc.createElement('div');		// icon not login
		c.setAttribute('style',	style_maker('display:inline-block; float:right; margin:8px 0 0 0;'));
		c.setAttribute('id', 		'uReadMe_not_login');
		b.appendChild(c);
		c = doc.createElement('div');		// icon is login
		c.setAttribute('style',	style_maker('display:inline-block; float:right; margin:8px 0 0 0;'));
		c.setAttribute('id', 		'uReadMe_is_login');
		b.appendChild(c);

		c = doc.createElement('div');
		c.setAttribute('id', 		'uReadMe_content');
		c.setAttribute('style',	style_maker('display:block; background-color:#FFFFFF; height:'+(a.clientHeight-b.clientHeight-1)+'px; '));
		a.appendChild(c);

		return c;
	}
	create_result = function (t, json) {
		ob = doc.getElementById('uReadMe_body');
		var p = ob.parentNode;
		p.removeChild(ob);
		ob = doc.createElement('div');
		ob.setAttribute('id',		'uReadMe_body');
		if (t == 'waiting') {
			ob.setAttribute('style',	style_maker('display:block; font-size:24px; font-family:微软雅黑; color:#ACF; padding-top:100px; text-align:center;'));
			ob.appendChild(doc.createTextNode("操作中，请稍候...")); 
		} else if (t == 'result') {
			ob.setAttribute('style',	style_maker('display:block; margin:30px 15% 30px 15%; border:1px solid #CCC; padding:50px; background-color:#FFF;'));
			var a = doc.createElement('div');
			a.setAttribute('style',	style_maker('font-size:24px; font-family:微软雅黑; color:#ACF; margin-right:30px;'));
			a.appendChild(doc.createTextNode("保存文章成功！")); 
			ob.appendChild(a);
			a = doc.createElement('a');
			a.setAttribute('style',	style_maker('color:#ACF; font-size:14px; cursor:pointer;'));
			a.setAttribute('href',	'http://uread.me/saved.php?vid='+json.article_id);
			a.appendChild(doc.createTextNode("查看文章")); 
			ob.appendChild(a);
			a = doc.createElement('a');
			a.setAttribute('style',	style_maker('color:#ACF; font-size:14px; margin:0 8px;'));
			a.appendChild(doc.createTextNode(" | ")); 
			ob.appendChild(a);
			a = doc.createElement('a');
			a.setAttribute('style',	style_maker('color:#ACF; font-size:14px; cursor:pointer;'));
			a.setAttribute('href',	'http://uread.me/saved.php?eid='+json.article_id);
			a.appendChild(doc.createTextNode("修改文章")); 
			ob.appendChild(a);
			a = doc.createElement('a');
			a.setAttribute('style',	style_maker('color:#ACF; font-size:14px; margin:0 8px;'));
			a.appendChild(doc.createTextNode(" | ")); 
			ob.appendChild(a);
			a = doc.createElement('a');
			a.setAttribute('style',	style_maker('color:#ACF; font-size:14px; cursor:pointer;'));
			a.setAttribute('href',	'http://uread.me/');
			a.appendChild(doc.createTextNode("进入uRead.Me")); 
			ob.appendChild(a);
		} else {
			ob.setAttribute('style',	style_maker('display:block; font-size:24px; font-family:微软雅黑; color:#FA6; padding-top:100px; text-align:center;'));
			ob.appendChild(doc.createTextNode(json.error_msg)); 
		}
		p.appendChild(ob);
	}
	close_layer = function () {
		var b = doc.getElementById('uReadMe_bg');
		if (b) doc.body.removeChild(b);
		var f = doc.getElementById('uReadMe_fg');
		if (f) doc.body.removeChild(f);
	}
	save = function () {
		if (hs) {
			var i	= 0;
			var ob 	= null;
			
			// -- 先获得save页面中的各个表单数据 -----------------------------------------------------------
			ob = doc.getElementsByName('uReadMe_circles');
			var c = '';
			for (i=0; i<ob.length; i++) {
				if (color2hex(ob[i].style.backgroundColor) != 'transparent') {  c += ob[i].attributes.uReadMeId.value + ',';  }
			}
			var cn = doc.getElementById('uReadMe_new_circle_name').value;
			var cp = (doc.getElementById('uReadMe_new_circle_is_private').checked) ? '1' : '';
			
			ob = doc.getElementsByName('uReadMe_friends');
			var f = '';
			for (i=0; i<ob.length; i++) {
				if (color2hex(ob[i].style.backgroundColor) != 'transparent') {  f += ob[i].attributes.uReadMeId.value + ',';  }
			}
			
			if (c || cn) {
				// hidding save button
				var s = doc.getElementById('uReadMe_save_button');
				s.style.display = 'none';
				
				// -- waiting to ajax post  -----------------------------------------------------------
				create_result('waiting');
	
				// AJAX POST -----------------------------------------------------------
				var param = '';
				param += 'u='		+ encodeURIComponent(doc.URL);
				param += '&t='		+ encodeURIComponent(doc.title);
				param += '&hs='		+ encodeURIComponent(hs);
				param += '&cn='		+ encodeURIComponent(cn);
				param += '&cp='		+ cp;
				param += '&c='		+ c;
				param += '&f='		+ f;
	
				xmlhttp.onreadystatechange=function() {
					if (xmlhttp.readyState==4) {
						if (xmlhttp.status==200) {
							try {
								var json = JSON.parse(xmlhttp.responseText);
							} catch (e) {
								json = JSON.parse( '{"error_msg":"can not parse json"}' );
							}
							if (json != null && json.article_id) {
								create_result('result', json);
							} else {
								create_result('error', {'error_msg':'保存文章时出现错误'});
							}
						} else {
							create_result('error', {'error_msg':'连接服务器出现错误'});
						}
					}
				}
				xmlhttp.open("POST", hst+"/save.php", true);
				xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
				xmlhttp.send(param);
			} else {
				alert('请选择一个阅读圈');
			}
		} else {
			create_error('无法获取页面内容，请刷新页面重新再试！');
		}
	}
	get_selection_HTML = function () {
		var s = doc.getSelection();
		if (s && s.rangeCount>0 && s.toString()) {
			var clonedSelection = '';
			var div = doc.createElement('div');
			for (var i=0; i<s.rangeCount; i++) {
				clonedSelection = s.getRangeAt(i).cloneRange().cloneContents();
				div.appendChild(clonedSelection);
			}
			s = div.innerHTML.replace(/<style(.|\n|\r)+?<\/style>|<link.+?>|<script(.|\n|\r)+?<\/script>|<iframe(.|\n|\r)+?<\/iframe>|<!--(.|\n|\r)+?-->|<form(.|\n|\r)+?<\/form>/gim, "");
			// parse STYLE and LINK from all HTML(Maybe part of), then add to selection
			var parser = doc.body.parentNode.outerHTML.match(/<style(.|\n|\r)+?<\/style>|<link.+?>/gim);
			for (var i=0; i<parser.length; i++) {
				s = parser[i] + s;
			}
			return s;
		} else return '';
	}
	get_all_HTML = function () {
		return doc.body.parentNode.outerHTML.replace(/<script(.|\n|\r)+?<\/script>|<iframe(.|\n|\r)+?<\/iframe>|<!--(.|\n|\r)+?-->|<form.*?>|<\/form>|<input.*?>/gim, "");
	}
	style_maker = function (style) {
		var s = {
			'background-color'		: 'transparent',
			'background-image'		: 'none',
			'background-position'	: '0% 0%',
			'background-repeat'		: 'repeat',
			'background-attachment'	: 'scroll',
			'border-top' 		: 'medium none transparent',
			'border-left' 		: 'medium none transparent',
			'border-right' 		: 'medium none transparent',
			'border-bottom' 	: 'medium none transparent',
			'outline'			: 'invert none medium',
			'color'				: '#000000',
			'direction'			: 'ltr',
			'letter-spacing'	: 'normal',
			'line-height'		: 'normal',
			'text-align'		: 'left',
			'text-decoration'	: 'none',
			'text-indent'		: '0',
			'text-transform'	: 'none',
			'white-space'		: 'normal',
			'word-spacing'		: 'normal',
			'font-family'		: 'Arial',
			'font-size'			: '12px',
			'font-style'		: 'normal',
			'font-variant'		: 'normal',
			'font-weight'		: 'normal',
			'margin'			: '0',
			'padding'			: '0',
			'list-style'		: 'disc outside none',
			'content'			: 'normal',
			'height'			: 'auto',
			'width'				: 'auto',
			'max-height'		: 'none',
			'max-width'			: 'none',
			'min-height'		: '0',
			'min-width'			: 'none',
			'display'			: 'inline',
			'top'				: 'auto',
			'left'				: 'auto',
			'right'				: 'auto',
			'bottom'			: 'auto',
			'clear'				: 'none',
			'clip'				: 'rect(auto,auto,auto,auto)',
			'cursor'			: 'auto',
			'float'				: 'none',
			'overflow'			: 'auto',
			'position'			: 'static',
			'vertical-align'	: 'bottom',
			'visibility'		: 'visible',
			'z-index'			: 'auto',
			'opacity'			: '1'
		};
		if (typeof(style) == 'string') {
			var split = style.split(';');
			var kv = '';
			for (var i=0; i<split.length; i++) {
				kv = split[i].split(':');
				s[kv[0]] = kv[1];
			}
		}
		var re = '';
		for (k in s) {
			re += k + ':' + s[k] + ';';
		}
		return re;
	}
	color2hex = function (colorStr) {
		var ret = "#";
		var tmp = '';
		var i = 0;
		if (colorStr == "transparent" || colorStr.substring(0, 1) == "#") {
			return colorStr;
		} else if (colorStr.substring(0, 4) == "rgb(") {
			colorStr = colorStr.substring(4, (colorStr.length - 1));
			colorStr = colorStr.split(",");
			for (i=0; i<3; i++) {
				tmp = parseInt(colorStr[i], 10).toString(16).toUpperCase();
				if (tmp.length == 1) {
					tmp = "0" + tmp;
				}
				ret += tmp;
			}
			return ret;
		} else if (colorStr.substring(0,5) == "rgba(") {
			colorStr = colorStr.substring(5, (colorStr.length - 1));
			colorStr = colorStr.split(",");
			if (colorStr[3] == 0) {
				return 'transparent';
			} else {
				for (i=0; i<3; i++) {
					tmp = parseInt(colorStr[i], 10).toString(16).toUpperCase();
					if (tmp.length == 1) {
						tmp = "0" + tmp;
					}
					ret += tmp;
				}
				return ret;
			}
		} else {
			return false;
		}
	}
	float_social = function (act, ob) {
		var v = ob.attributes.uReadMeSocials.value;
		if (typeof(v) == 'string' && v.length>0) {
			var c = doc.getElementById('uReadMe_social_float');
			if (!c) {
				c = doc.createElement('div');
				c.setAttribute('id', 	'uReadMe_social_float');
				c.setAttribute('style',	style_maker('display:none; position:absolute; top:0; left:0; background-color:#FFE; padding:5px 10px; border:1px solid #CCC; '));
				var d = doc.createElement('img');
				d.setAttribute('id', 	'uReadMe_social_weibo');
				d.setAttribute('src', 	'http://uread.me/images/icon_weibo_16.png');
				d.setAttribute('style',	style_maker('display:none; width:16px; height:16px; margin:5px;'));
				c.appendChild(d);
				d = doc.createElement('img');
				d.setAttribute('id', 	'uReadMe_social_renren');
				d.setAttribute('src', 	'http://uread.me/images/icon_renren_16.png');
				d.setAttribute('style',	style_maker('display:none; width:16px; height:16px; margin:5px;'));
				c.appendChild(d);
				d = doc.createElement('img');
				d.setAttribute('id', 	'uReadMe_social_kaixin001');
				d.setAttribute('src', 	'http://uread.me/images/icon_kaixin001_16.png');
				d.setAttribute('style',	style_maker('display:none; width:16px; height:16px; margin:5px;'));
				c.appendChild(d);
				d = doc.createElement('img');
				d.setAttribute('id', 	'uReadMe_social_qqwb');
				d.setAttribute('src', 	'http://uread.me/images/icon_qqwb_16.png');
				d.setAttribute('style',	style_maker('display:none; width:16px; height:16px; margin:5px;'));
				c.appendChild(d);
				d = doc.createElement('span');
				d.setAttribute('id', 	'uReadMe_social_name');
				d.setAttribute('style',	style_maker('display:none; margin:5px;'));
				c.appendChild(d);
				doc.getElementById('uReadMe_friends_div').appendChild(c);
			}
			if (act == 'show') {
				var s = v.split('_');
				for (var i=0; i<s.length; i++) {
					doc.getElementById('uReadMe_social_' + s[i]).style.display = 'inline-block';
				}
                                var n = doc.getElementById('uReadMe_social_name');
                                n.parentNode.removeChild(n);
                                var nn = doc.createElement('span');
				nn.setAttribute('id', 	'uReadMe_social_name');
				nn.setAttribute('style',	style_maker('display:inline-block; margin:5px;'));
                                nn.appendChild(doc.createTextNode(ob.innerHTML));
                                doc.getElementById('uReadMe_social_float').appendChild(nn);
				c.style.display = 'block';
				c.style.top  = (ob.offsetTop - doc.getElementById('uReadMe_body').scrollTop - c.clientHeight + 3) + 'px';
				c.style.left  = (ob.offsetLeft) + 'px';
			} else {
				c.style.display = 'none';
				doc.getElementById('uReadMe_social_name').style.display = 'none';
				doc.getElementById('uReadMe_social_weibo').style.display = 'none';
				doc.getElementById('uReadMe_social_renren').style.display = 'none';
				doc.getElementById('uReadMe_social_kaixin001').style.display = 'none';
				doc.getElementById('uReadMe_social_qqwb').style.display = 'none';
			}
		}
	}
}